package ms.platform.dict.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ms.platform.dict.dao.DictDao;
import ms.platform.dict.model.DictModel;
import ms.platform.enumdata.DictType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation=Propagation.SUPPORTS)
public class DictService {

	@Autowired
	private DictDao dictDao;
	@Autowired
	private DictToMapService dictToMapService;

	/**
	 * 修改角色
	 * @param dict
	 */
	public void update(DictModel dict) {
		dictDao.update(dict);
	}

	/**
	 * 新增分组
	 * @param dict
	 */
	public void insert(DictModel dict) {
		dictDao.insert(dict);
	}
	
	/**
	 * 新增数据
	 * @param dict
	 */
	public void insert1(DictModel dict) {
		dict.setLevel("1");
		dict.setCreateDate(new Date());
		dictDao.insert(dict);
	}

	/**
	 * 根据id删除
	 * @param id
	 */
	public void deleteById(Integer id) {
		dictDao.deleteById(id);
	}
	
	/**
	 * 带条件分页查询list
	 * @param map
	 * @return
	 */
	public List<DictModel> queryDictListByCondition(Map<String, Object> map) {
		List<DictModel> queryDictListByCondition = dictDao.queryDictListByCondition(map);
		Map<String, String> productTypeMap = dictToMapService
				.getMapByDictType(DictType.DICT_STATUS.getKey());
		for (DictModel dictModel : queryDictListByCondition) {
			dictModel.setStatus(productTypeMap.get(dictModel.getStatus()));
		}
		return queryDictListByCondition;
	}

	/**
	 * 分页查询获取总条数
	 * @param map
	 * @return
	 */
	public Integer queryDictCount(Map<String, Object> map) {
		return dictDao.queryDictCount(map);
	}
	/**
	 * 根据id查询字典
	 * @param id
	 * @return
	 */
	public DictModel queryDictById(Integer id) {
		return dictDao.queryDictById(id);
	}

	public void deleteGroupById(DictModel dict) {
		dictDao.deleteGroupById(dict);
	}

	public DictModel queryGroupCodeByName(String groupCode) {
		return dictDao.queryGroupCodeByName(groupCode);
	}
	
	public DictModel findDictByType(String dictType) {
		return dictDao.findDictByType(dictType);
	}
	
	public List<DictModel> queryGroupCode(Map<String, Object> map){
		return dictDao.queryGroupCode(map);
	}
	public List<DictModel> getDictByParentId(Integer parentId) {
		return dictDao.getDictByParentId(parentId);
	}
	
	
	/**
	 * 根据父节点的字典编码查询子节点数据(只包含字典编码和字典名称)
	 */
	public List<DictModel> queryChildByParentDictCode(Map<String, Object> map){
		return dictDao.queryChildByParentDictCode(map);
	}

	public void updateStatus(DictModel dict) {
		dictDao.updateStatus(dict);
	}

	public DictModel queryByStatus(String string) {
		return dictDao.queryByStatus(string);
	}

	public List<DictModel> queryByParentId(int Id) {
		return dictDao.queryByParentId(Id);
	}
	//根据字典编码查数据
	public DictModel queryByDictCode(String dictCode) {
		return dictDao.queryByDictCode(dictCode);
	}
	
	/**
	 * 方法描述：根据产品id查询产品投保人与被投保人的关系列表
	 */
	public List<DictModel> queryDictCodeByProductId(Map<String, Object> map){
		return dictDao.queryDictCodeByProductId(map);
	}
	
	public DictModel queryByDictCodeAndInsuredRelation(String dictCode,
			Integer parentIdTo) {
		String parentId = Integer.toString(parentIdTo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictCode", dictCode);
		map.put("parentId", parentId);
		return dictDao.queryByDictCodeAndInsuredRelation(map);
	}
	
	public List<DictModel> selectDictListByDictCodeString (String dictCode,
	String parentId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictCode", dictCode);
		map.put("parentId", parentId);
		return dictDao.selectDictListByDictCode(map);
	}

	public DictModel queryByDictNameAndInsuredRelation(String dictName, Integer parentId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictName", dictName);
		map.put("parentId", parentId);
		return dictDao.queryByDictNameAndInsuredRelation(map);
	}
	
	public List<DictModel> selectDictListByCode(Map<String, Object> map){
		return dictDao.selectDictListByCode(map);
	}
	
	
	public List<DictModel> queryDictListByCode(Map<String, Object> map){
		return dictDao.queryDictListByCode(map);
	}
	
	public DictModel queryDictByDictCode(Map<String, Object> map){
		return dictDao.queryDictByDictCode(map);
	}
}
